package com.own.component.util.database.mysql;

import com.alibaba.druid.sql.dialect.mysql.parser.MySqlCreateTableParser;
import com.alibaba.druid.sql.parser.SQLCreateTableParser;
import com.own.component.util.database.base.BaseDatabaseFieldMapping;
import com.own.component.util.database.base.BaseDatabaseProperty;
import com.own.component.util.database.base.model.DatabaseEntryField;

/**
 * MysqlProperty
 *
 * @author chenxueli
 * @date 2022-10-15 14:02:56
 */
public class MysqlProperty extends BaseDatabaseProperty {

    private static final String PRI = "PRI";
    private static final String YES = "YES";
    private static MysqlProperty INSTANCE;

    /**
     * 私有化构造方法
     */
    private MysqlProperty() {
        // 放入字段名称和映射关系信息
        super(
                DatabaseEntryField.builder()
                        .tableName("table_name")
                        .tableComment("table_comment")
                        .columnName("column_name")
                        .columnComment("column_comment")
                        .type("data_type")
                        .length("character_maximum_length")
                        .isNull("is_nullable")
                        .nullHandler(YES::equals)
                        .isPrimaryKey("column_key")
                        .primaryKeyHandler(PRI::equals)
                        .defaultValue("column_default")
                        .build(),
                MysqlFieldMappingEnum.values()
        );
    }

    /**
     * 获取数据库类型信息
     *
     * @return 数据库类型
     */
    public static BaseDatabaseProperty instance() {
        if (INSTANCE == null) {
            INSTANCE = new MysqlProperty();
        }
        return INSTANCE;
    }

    /**
     * 处理创建sql的的解析器
     *
     * @param sql sql语句
     * @return 解析器
     */
    @Override
    public SQLCreateTableParser createTableParser(String sql) {
        return new MySqlCreateTableParser(sql);
    }

    /**
     * 驱动信息
     */
    @Override
    public String driver() {
        return "com.mysql.cj.jdbc.Driver";
    }

    /**
     * 地址信息
     */
    @Override
    public String connectionUrl() {
        return "jdbc:mysql://{host}:{port}/{database}?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true";
    }

    /**
     * 数据库名称的获取正则
     *
     * @return 数据库名称的获取正则
     */
    @Override
    public String databaseRegex() {
        return "jdbc:(?<db>\\\\w+):.*((//)|@)(?<host>.+):(?<port>\\\\d+)(/|(;DatabaseName=)|:)(?<dbName>\\\\w+.+)\\\\?";
    }

    /**
     * 所有表查询语句
     */
    @Override
    public String tablesSql() {
        return "select table_name, table_comment from information_schema.tables where table_schema = ?";
    }

    /**
     * 所有表字段查询语句
     */
    @Override
    public String tablesColumnSql() {
        return "select table_name, is_nullable, column_key, column_default, column_name, column_comment, data_type, character_maximum_length from information_schema.columns where table_schema = ?";
    }

    /**
     * 默认的映射类型
     *
     * @return 默认的映射类型
     */
    @Override
    public BaseDatabaseFieldMapping defaultFieldType() {
        return MysqlFieldMappingEnum.DEFAULT;
    }

}
